package com.fh.shop.admin.aspect;

import com.fh.shop.admin.annotation.LogInfo;
import com.fh.shop.admin.biz.log.ILogService;
import com.fh.shop.admin.po.log.Log;
import com.fh.shop.admin.common.SystemContant;
import com.fh.shop.admin.po.user.User;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/*@Component
@Aspect*/
public class LogAspect {
    
    @Resource(name = "logInfoService")
    private ILogService logInfoService;
    
    public Object doInfo(ProceedingJoinPoint pjp) throws Throwable {

        //获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //获取session中的值
        User user = (User) request.getSession().getAttribute(SystemContant.CURR_USER);
        String userName = "";
        String realName = "";
        //注销用
        if (user != null){
            userName = user.getUserName();
            realName = user.getRealName();
        }


        //执行真正的方法
        Object result = pjp.proceed();

        //获取自定义注解的方法信息
        String info = getInfo(pjp);


        //获取session中的值
        user = (User) request.getSession().getAttribute(SystemContant.CURR_USER);
        if (user != null){
            userName = user.getUserName();
            realName = user.getRealName();
        }


        //获取登录用户的所有信息
        String detail = getLoginInfo(request);


        //放入logInfo中，添加到数据库
        extracted(userName, realName, info, detail);

        return result;
    }

    //获取自定义注解的方法信息
    private String getInfo(ProceedingJoinPoint pjp) {
        //获取类的全路径
//        String clzzName = pjp.getTarget().getClass().getCanonicalName();
        //获取方法签名
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        //根据方法签名获取方法名
//        String methodName = signature.getName();
        //根据方法签名获取方法
        Method method = signature.getMethod();

        //判断方法上是否有自定义的注解
        String info = "";
        if (method.isAnnotationPresent(LogInfo.class)){
            //获取注解中的值
            LogInfo annotation = method.getAnnotation(LogInfo.class);
            info = annotation.info();
        }
        return info;
    }

    //获取登录用户信息并且遍历分割存入日志的数据库
    private String getLoginInfo(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        StringBuilder stringBuilder = new StringBuilder();
        if (parameterMap != null && parameterMap.size()>0){
            //entrySet:转换为set集合
            //iterator:使用迭代器遍历
            Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, String[]> next = iterator.next();
                String key = next.getKey();
                String[] value = next.getValue();
                stringBuilder.append(";").append(key).append("=").append( StringUtils.join(value,","));
            }
        }
        String detail = "";
        if (stringBuilder.length()>0){
            detail = stringBuilder.substring(1);
        }
        return detail;
    }

    //向数据库插入数据
    private void extracted(String userName, String realName, String info, String detail) {
        Log log = new Log();
        log.setRealName(realName);
        log.setUserName(userName);
        log.setInfoTime(new Date());
        log.setInfo(info);//放入注解信息
        log.setDetail(detail);//放入操作的数据
        logInfoService.addLogInfo(log);
    }

    /*@Before(value = "execution(* com.fh.shop.admin.controller..*.*(..)")
    public void before(){
        System.out.println("方法前~~~~~~~~~~~~~~~~~~~~~~~");
    }*/
}
